#include "base64_widget.h"
#include "widget.h"
#include "base64.h"
#include <QStringList>

CBase64Widget::CBase64Widget(QWidget *parent) : QWidget(parent)
{
    m_widgetMain = (Widget *)parent;

    if (!InitUi()) {
        m_widgetMain->MsgBox("Base64::InitUi error!!\n");
        return;
    }

    if (!InitSignals()) {
        m_widgetMain->MsgBox("Base64::InitSignals error!!\n");
        return;
    }
}

CBase64Widget::~CBase64Widget()
{
    DeleteSignals();
    DeleteUi();
}

bool CBase64Widget::InitUi(void)
{
    if (!InitUiEncode()) {
        m_widgetMain->MsgBox("Base64::InitUiEncode error!\n");
        return false;
    }

    if (!InitUiMode()) {
        m_widgetMain->MsgBox("Base64::InitUiMode error!\n");
        return false;
    }

    if (!InitUiDisplay()) {
        m_widgetMain->MsgBox("Base64::InitUiDisplay error!\n");
        return false;
    }

    return true;
}

void CBase64Widget::DeleteUi(void)
{
    DeleteUiDisplay();
    DeleteUiMode();
    DeleteUiEncode();
}

bool CBase64Widget::InitData(void)
{
    return true;
}

void CBase64Widget::DeleteData(void)
{
}

bool CBase64Widget::InitUiEncode(void)
{
    m_labelEncode = new QLabel(this);
    m_labelEncode->setGeometry(50, 20, 60, 20);
    m_labelEncode->setText(FROMLOCAL("编解码"));

    m_radioEncode = new QRadioButton(this);
    m_radioEncode->setGeometry(110, 20, 50, 20);
    m_radioEncode->setText(FROMLOCAL("编码"));
    m_radioEncode->click();

    m_radioDecode = new QRadioButton(this);
    m_radioDecode->setGeometry(160, 20, 50, 20);
    m_radioDecode->setText(FROMLOCAL("解码"));

    m_groupEncode = new QButtonGroup(this);
    m_groupEncode->addButton(m_radioEncode, 1);
    m_groupEncode->addButton(m_radioDecode, 2);

    return true;
}

void CBase64Widget::DeleteUiEncode(void)
{
    if (NULL != m_groupEncode) {
        delete m_groupEncode;
        m_groupEncode = NULL;
    }

    if (NULL != m_radioEncode) {
        delete m_radioEncode;
        m_radioEncode = NULL;
    }

    if (NULL != m_radioDecode) {
        delete m_radioDecode;
        m_radioDecode = NULL;
    }

    if (NULL != m_labelEncode) {
        delete m_labelEncode;
        m_labelEncode = NULL;
    }

    return;
}

bool CBase64Widget::InitUiMode(void)
{
    m_labelMode = new QLabel(this);
    m_labelMode->setGeometry(50, 50, 60, 20);
    m_labelMode->setText(FROMLOCAL("模式"));

    m_radioHex = new QRadioButton(this);
    m_radioHex->setGeometry(110, 50, 50, 20);
    m_radioHex->setText(FROMLOCAL("hex"));
    m_radioHex->click();

    m_radioAsc = new QRadioButton(this);
    m_radioAsc->setGeometry(160, 50, 50, 20);
    m_radioAsc->setText(FROMLOCAL("asc"));

    m_groupMode = new QButtonGroup(this);
    m_groupMode->addButton(m_radioEncode, 1);
    m_groupMode->addButton(m_radioDecode, 2);

    return true;
}

void CBase64Widget::DeleteUiMode(void)
{
    if (NULL != m_groupMode) {
        delete m_groupMode;
        m_groupMode = NULL;
    }

    if (NULL != m_radioAsc) {
        delete m_radioAsc;
        m_radioAsc = NULL;
    }

    if (NULL != m_radioHex) {
        delete m_radioHex;
        m_radioHex = NULL;
    }

    if (NULL != m_labelMode) {
        delete m_labelMode;
        m_labelMode = NULL;
    }
}

bool CBase64Widget::InitUiDisplay(void)
{
    // 转化按钮
    m_buttonConvert = new QPushButton(this);
    m_buttonConvert->setGeometry(385, 320, 30, 20);
    m_buttonConvert->setText(FROMLOCAL("转化"));

    // 校验前
    m_editBefore = new QTextEdit(this);
    m_editBefore->setGeometry(20, 90, 360, 400);
    m_editBefore->setReadOnly(false);

    // 校验后
    m_editAfter = new QTextEdit(this);
    m_editAfter->setGeometry(420, 90, 360, 400);
    m_editAfter->setReadOnly(true);
    m_editAfter->setReadOnly(true);
    return true;
}

void CBase64Widget::DeleteUiDisplay(void)
{
    if (NULL != m_buttonConvert) {
        delete m_buttonConvert;
        m_buttonConvert = NULL;
    }

    if (NULL != m_editBefore) {
        delete m_editBefore;
        m_editBefore = NULL;
    }

    if (NULL != m_editAfter) {
        delete m_editAfter;
        m_editAfter = NULL;
    }
}

bool CBase64Widget::InitSignals(void)
{
    connect(m_radioEncode, SIGNAL(clicked()), this, SLOT(RadioChangeSlot()));
    connect(m_radioDecode, SIGNAL(clicked()), this, SLOT(RadioChangeSlot()));
    connect(m_radioHex, SIGNAL(clicked()), this, SLOT(RadioChangeSlot()));
    connect(m_radioAsc, SIGNAL(clicked()), this, SLOT(RadioChangeSlot()));
    connect(m_buttonConvert, SIGNAL(clicked()), this, SLOT(ConvertionSlot()));
    connect(m_editBefore, SIGNAL(textChanged()), this, SLOT(ConvertionSlot()));

    return true;
}

void CBase64Widget::DeleteSignals(void)
{
    disconnect(m_radioEncode, SIGNAL(clicked()), this, SLOT(RadioChangeSlot()));
    disconnect(m_radioDecode, SIGNAL(clicked()), this, SLOT(RadioChangeSlot()));
    disconnect(m_radioHex, SIGNAL(clicked()), this, SLOT(RadioChangeSlot()));
    disconnect(m_radioAsc, SIGNAL(clicked()), this, SLOT(RadioChangeSlot()));
    disconnect(m_buttonConvert, SIGNAL(clicked()), this,
               SLOT(ConvertionSlot()));
    disconnect(m_editBefore, SIGNAL(textChanged()), this,
               SLOT(ConvertionSlot()));
}

void CBase64Widget::RadioChangeSlot(void)
{
    if (NULL != m_radioHex) {
        if (m_radioHex->isChecked()) {
            printf("111\n");
        }
        else {
            printf("112\n");
        }
    }
}

void CBase64Widget::ConvertionSlot(void)
{
    // 清空
    m_editAfter->clear();
    // 获取
    QString str = GetConvertString();
    // 显示
    m_editAfter->setText(str);
}

QString CBase64Widget::GetConvertString(void)
{
    QString str = m_editBefore->toPlainText();
    if (str.isEmpty())
        return str;

    unsigned char *pbuf = new unsigned char[str.count() * 2 + 20];
    char *poutbuf = new char[str.count() * 2 + 20];
    unsigned int buflen = 0;

    if (m_radioHex->isChecked() && m_radioEncode->isChecked()) {
        QString sep = " ";
        QStringList list = str.split(QRegExp(sep));
        for (int i = 0; i < list.count(); i++) {
            bool ok;
            QString s = list.at(i);
            unsigned char tmp = s.toInt(&ok, 16);
            if (ok) {
                pbuf[buflen++] = tmp;
            }
        }

        printf("1111\n");
    }
    else {
        char *ps = str.toLatin1().data(); // 先 qs.toLatin1()转为 QByteArry
        strcpy((char *)pbuf, ps);
        buflen = strlen(ps);

        printf("1112 %s\n", pbuf);
    }

    // 加密
    unsigned int len = 0;
    QString out = "";
    if (m_radioEncode->isChecked()) {
        printf("encode\n");
        len = base64_encode(pbuf, buflen, poutbuf);
        out = QString(poutbuf);
    }
    else {
        printf("decode\n");
        len = base64_decode((char *)pbuf, buflen, (unsigned char *)poutbuf);
        if (m_radioHex->isChecked()) {
            printf("decode hex\n");
            for (unsigned int i = 0; i < len; i++) {
                out += QString("%1").arg((unsigned char)poutbuf[i], 2, 16,
                                         QChar('0')) +
                       " ";
            }
        }
        else {
            printf("decode asc\n");
            out = QString(poutbuf);
        }
    }

	if(NULL != pbuf){
		delete pbuf;
		pbuf = NULL;
	}

	if(NULL != poutbuf){
		delete poutbuf;
		poutbuf = NULL;
	}

    return out;
}
